// ============================== 导入依赖 ============================== //

import CORE from '@/CORE'
import Client from '@api/equipment/api/AssetChangeApi'

import ComClass from '@api/ComQuery'
import ListClass from '@api/equipment/model/AssetChangeLifeListReq'

const CLIENT = CORE.grpcCLIENT(Client)
const COM = [ComClass, 'comQuery']

// ============================== 导出远程接口 ============================== //

export default {
  // —————————————————————————————— 列表 —————————————————————————————— //

  async getList(param, TIP = '查询全生命周期') {
    const DTO = CORE.grpcDTO(ListClass, param, COM)
    const resp = await CORE.grpc(TIP, CLIENT, 'queryAssetChangeLifeList', DTO)
    return [resp.comQuery.totalCount, resp.operateDtoList || []]
  },
  transList(arr, { state: { DICT } }) {
    arr.forEach((item) => {
      const record = (item.changeOperateDtoList || [])[0] || {}
      item.id = `${item.id}-${record.id || ''}`
      item.operate = record.operateTypeName
      item.content = transContent(item, record, DICT)
    })
    return arr
  },
}

// ============================== 内部方法 ============================== //

const transContent = (obj, record, { TYPE, AUDIT, CHANGE_STATUS }) => {
  const { changeType: type } = obj
  const { operateType: operate, approvalResult, approvalView } = record

  // 申请
  if (operate === 1) {
    const name = CORE.dict(TYPE, type, 2)
    const organ = `${name}${CORE.MAP.ORGAN}：${obj.applicantDeptName}`
    const user = `申请人：${obj.applicantName}`
    const remark = `备注：${obj.remark}`

    if (type === 1 || type === 2) {
      return `${organ}，${user}，${remark}`
    } else if (type === 3) {
      const organOut = organ.replace(/^.{2}/, '调出')
      const organIn = `调入${CORE.MAP.ORGAN}：${obj.allocationDeptName}`
      return `${organOut}，${organIn}，${user}，${remark}`
    } else if (type === 4) {
      // const status = `${name}状态：${CORE.dict(CHANGE_STATUS, obj.alterType)}：${obj.allocationDeptName}`
      return `${organ}，${user}，${remark}`
    }
  }

  // 审批
  if (operate === 2) {
    return `审批结果：${CORE.dict(AUDIT, approvalResult)}，审批意见：${approvalView}`
  }

  // 完成
  if (operate === 3) {
    return `审批结果：，审批意见：${approvalView}`
  }
}
